<?php
/**
 * MySQL 操作类
 * User: lisgroup
 * Time: 2017/5/21 15:58
 */
namespace lisfw\DB;

class Mysql implements DbInterface {

    //数据库连接
    private $_conn = null;

    public function __construct($configKey = 'DB_CONFIG')
    {
        if (is_null($this->_conn)) {
            $dbConfig = config($configKey);
            $this->_conn = mysqli_connect($dbConfig['DB_HOST'], $dbConfig['DB_USER'], $dbConfig['DB_PASSWORD'], $dbConfig['DB_NAME'], $dbConfig['DB_PORT']);
        }
    }

    //查询方法
    public function query($sql){
        $result = mysqli_query($this->_conn, $sql);
        return $result;
    }

    public function getAll($sql)
    {
        $result = $this->query($sql);
        $arr = array();
        while ($row = mysqli_fetch_assoc($result)) {
            $arr[] = $row;
        }
        return $arr;
    }
    public function getCol($sql)
    {
        $result = $this->query($sql);
        $arr = array();
        while ($row = mysqli_fetch_assoc($result)) {
            $arr[] = $row[0];
        }
        return $arr;
    }

    public function getRow($sql)
    {
        $res = $this->query($sql);
        return mysqli_fetch_assoc($res);
    }
    public function getOne($sql)
    {
        $res = $this->query($sql);
        $row = mysqli_fetch_row($res);
        return $row[0];
    }

    public function close()
    {
        mysqli_close($this->_conn);
    }
}